package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.User;
import com.battle.pojo.entity.WarCaseStudyUser;
import com.battle.pojo.param.warCaseStudyUser.*;
import com.battle.pojo.vo.warCaseStudyUser.*;
import com.battle.pojo.dto.warCaseStudyUser.*;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.repository.in.WarCaseStudyUserRepository;
import com.battle.service.service.in.WarCaseStudyUserService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：战例研讨用户服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class WarCaseStudyUserServiceImpl implements WarCaseStudyUserService {

    private final WarCaseStudyUserRepository warCaseStudyUserRepository;
    private final UserRepository userRepository;

    public WarCaseStudyUserServiceImpl(WarCaseStudyUserRepository warCaseStudyUserRepository, UserRepository userRepository) {
        this.warCaseStudyUserRepository = warCaseStudyUserRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建战例研讨用户
     *
     * @param param
     */
    @Override
    public void createWarCaseStudyUser(CreateWarCaseStudyUserParam param) {
        WarCaseStudyUser warCaseStudyUser = new WarCaseStudyUser();
        BeanUtils.copyProperties(param, warCaseStudyUser);
        Long id = IdGenerator.SNOW.generate();
        warCaseStudyUser.setId(id);
        warCaseStudyUserRepository.create(warCaseStudyUser);
    }

    /**
     * 修改战例研讨用户
     *
     * @param param
     */
    @Override
    public void editWarCaseStudyUser(EditWarCaseStudyUserParam param) {
        Long id = param.getId();
        WarCaseStudyUser warCaseStudyUser = warCaseStudyUserRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, warCaseStudyUser);
        warCaseStudyUserRepository.edit(warCaseStudyUser);
    }

    /**
     * 移除战例研讨用户
     *
     * @param id
     */
    @Override
    public void removeWarCaseStudyUser(Long id) {
        warCaseStudyUserRepository.removeById(id);
    }

    /**
     * 启动/禁用战例研讨用户
     *
     * @param id
     */
    @Override
    public void enabledWarCaseStudyUser(Long id) {
        WarCaseStudyUser warCaseStudyUser = warCaseStudyUserRepository.getByIdCheck(id);
        warCaseStudyUser.setIsDisable(!warCaseStudyUser.getIsDisable());
        warCaseStudyUserRepository.edit(warCaseStudyUser);
    }

    /**
     * 获取战例研讨用户详情
     *
     * @param id
     * @return
     */
    @Override
    public GetWarCaseStudyUserVo getWarCaseStudyUser(Long id) {
        WarCaseStudyUser warCaseStudyUser = warCaseStudyUserRepository.getByIdCheck(id);
        GetWarCaseStudyUserVo vo = new GetWarCaseStudyUserVo();
        BeanUtils.copyProperties(warCaseStudyUser, vo);
        return vo;
    }

    /**
     * 获取战例研讨用户 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetWarCaseStudyUserListVo> getWarCaseStudyUserList(GetWarCaseStudyUserListParam param) {

        SelectWarCaseStudyUserDto dto = new SelectWarCaseStudyUserDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetWarCaseStudyUserListVo> page = warCaseStudyUserRepository.selectWarCaseStudyUser(dto);
        List<GetWarCaseStudyUserListVo> warCaseStudyUserList = page.getRecords();
        List<GetWarCaseStudyUserListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(warCaseStudyUserList)) {
            List<Long> userIds = new ArrayList<>();

            warCaseStudyUserList.forEach(v -> {
                userIds.add(v.getInitiatorId());
                userIds.add(v.getUserId());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);

            for (GetWarCaseStudyUserListVo warCaseStudyUser : warCaseStudyUserList) {
                GetWarCaseStudyUserListVo vo = new GetWarCaseStudyUserListVo();
                BeanUtils.copyProperties(warCaseStudyUser, vo);
                User operation = userMap.get(warCaseStudyUser.getInitiatorId());
                if (operation != null) {
                    vo.setInitiatorName(operation.getName());
                }
                User user = userMap.get(warCaseStudyUser.getUserId());
                if (user != null) {
                    vo.setUserName(user.getName());
                }
                list.add(vo);
            }
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取战例研讨用户下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetWarCaseStudyUserDownListVo> getWarCaseStudyUserDownList(GetWarCaseStudyUserDownListParam param) {
        WarCaseStudyUser po = new WarCaseStudyUser();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<WarCaseStudyUser> warCaseStudyUserList = warCaseStudyUserRepository.getListByEntity(po);

        List<GetWarCaseStudyUserDownListVo> list = new ArrayList<>();
        for (WarCaseStudyUser warCaseStudyUser : warCaseStudyUserList) {
            GetWarCaseStudyUserDownListVo vo = new GetWarCaseStudyUserDownListVo();
            BeanUtils.copyProperties(warCaseStudyUser, vo);
            list.add(vo);
        }
        return list;
    }


}



